package com.longge;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-04-16 01:46
 */
public class SplitListToParts {

    public ListNode[] splitListToParts(ListNode root, int k) {
        ListNode[] ans = new ListNode[k];
        if (root == null) return ans;
        int size = 0;
        ListNode cur = root;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        int n = size / k;
        int m = size % k;
        cur = root;
        for (int i = 0; i < k; i++) {
            ListNode head = cur;
            for (int j = 0; j < n + (i < m ? 1 : 0) - 1; ++j) {
                if (cur != null) cur = cur.next;
            }
            if (cur != null) {
                ListNode pre = cur;
                cur = cur.next;
                pre.next = null;
            }
            ans[i] = head;
        }
        return ans;

    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(1);
        node1.next = node2;
        node2.next = node3;
        new SplitListToParts().splitListToParts(node1, 2);
    }
}
